// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境

// 云函数入口函数
exports.main = async (event, context) => {
  return cloud.callFunction({
    name: "model_student",
    data: {
      op: "select",
      key: "u_id",
      value: event.user_id
    }
  }).then(res => {
    var studentList = res.result.data;
    for (var i = 0; i < studentList.length; i++) {
      var default_value = "0";
      if (studentList[i]._id == event.default_student_id) {
        default_value = "1";
      }
      // console.log(studentList[i]._id);
      cloud.callFunction({
        name: "model_student",
        data: {
          op: "update",
          id: studentList[i]._id,
          u_id: studentList[i].u_id,
          i_id: studentList[i].i_id,
          name: studentList[i].name,
          sex: studentList[i].sex,
          height: studentList[i].height,
          weight: studentList[i].weight,
          grade: studentList[i].grade,
          class: studentList[i].class,
          special: studentList[i].special,
          bust: studentList[i].bust,
          waist: studentList[i].waist,
          hip: studentList[i].hip,
          default: default_value
        }
      }).then(res => { console.log(res.result); })
    }
    // return res.result.data;
  }).then(res => {
    return cloud.callFunction({
      name: "model_student",
      data: {
        op: "select",
        key: "u_id",
        value: event.user_id
      }
    }).then(res => { return res.result.data; })
  })
}